$(function() {
	//用户如果存在，则进行一系列操作
	if($.cookie("userSession")) {
		$.ajax({
			type: "get",
			url: "https://api.leancloud.cn/1.1/users/me",
			headers: {
				"X-LC-Id": "9keh2YX5j8hpDc0YR2REhv0q-gzGzoHsz",
				"X-LC-Key": "rrdO8OEJACH5qna3m3FzhpYf",
				"X-LC-Session": $.cookie("userSession")
			},
			success: function(data) {
				console.log(data);
				var rolerId = data.Roler.objectId;
				$("#username").html("【欢迎回来，亲爱的" + panduan(rolerId) + "】<b>" + data.username + "</b>");
				//成功时判断当前用户为买家还是卖家，进行权限管理

				//							getrelations()
			},
			error: function(e) {
				var code = JSON.parse(e.responseText).code;
				console.log(code);
				for(var i in errocode) {
					if(errocode[i].code == code) {
						console.log(errocode[i].content)
					}
				}
			}
		});
	} else {
		location.href = "my_register.html";
	}
});
$("#loginout").on("click", function() {
	$.removeCookie("userSession");
	location.href = "my_register.html";
});

function panduan(userObjectId) {
	//卖家
	if(userObjectId == "58caa211128fe1006c80392f") {
		$("#selcar").attr("href", "secondCarInit.html");
		var str = "卖家";
		return str;
	} else if(userObjectId == "58caa22c44d90400682bae0b") {
		//买家
		$("#selcar").attr("href", "my_maiche.html");
		var str = "买家";
		return str;
	}

};